<?php
namespace app\index\controller;
use extend\Page;
use extend\Upload;
use lib\Response;
use lib\Session;
use lib\Db;

class Diary extends Common{

    /**
     * 创建日记本
     */
    function create(){
        return $this->fetch();
    }

    /**
     * 日记列表
     */
    function index(){
        return $this->fetch();
    }

    /**
     * 日记详情
     */
    function detail(){
        $bid = $this->request->get['bid'];
        if(!is_numeric($bid)){
            $this->error('获取bid失败!');
        }
        $book = Db::name('book')->where(['id'=>$bid])->find();
        $list = Db::name('diary')->where(['bid'=>$bid])->select();
        $this->set('book', $book);
        $this->set('list', $list);
        return $this->fetch();
    }

    /**
     * 写日记
     */
    function write(){
        $book = Db::name('book')->field('id, name')->select();
        $this->set('book', $book);
        return $this->fetch();
    }

    /**
     * 新建日记本数据写入数据库
     * @throws \Lib\Exception
     */
    function createBook(){
        if(!Session::get('uid')){
	        return $this->error('获取UID失败！');
        }
        $params = $this->request->post;
        params_check(array('name', 'auth', 'desc'));
	    $data = array(
		    'uid'   => Session::get('uid'),
		    'name'  => trim($params['name']),
		    'intro'  => strip_tags($params['desc']),
		    'auth'  => $params['auth'],
		    'time'  => date('Y-m-d H:i:s')
	    );
        if($this->request->file('image')){
	        $upload = new Upload(['image']);
	        $res = $upload->move();
	        $data['cover'] = $res['image']['relative'];
        }
	    $result = Db::name('book')->insert($data);
        if($result){
	        return $this->success('创建日记本成功！', url('Diary/book'));
        }else{
	        return $this->error('创建日记本失败！', url('Diary/create'));
        }
    }

    /**
     * 新增日记
     * @return array|string
     * @throws \Exception
     */
    function writeDiary(){
	    if(!Session::get('uid')){
		    return $this->error('获取UID失败！');
	    }
        $params = $this->request->post;
        params_check(array('book', 'content'));
        if(!is_numeric($params['book'])){
            return Response::error(('非法的日记本ID!'));
        }
        if(empty($params['content'])){
            return Response::error(('请填写日记内容!'));
        }
        $data = array(
	        'book_id'  => $params['book'],
	        'uid'  => Session::get('uid'),
	        'content'  => htmlspecialchars($params['content']),
	        'time'  => date('Y-m-d H:i:s')
        );
	    $diaryId = Db::name('diary')->insert($data, true);
	    if($this->request->file('image')){
		    $upload = new Upload(['image']);
		    $res = $upload->move();
		    Db::name('diary_pic')->insert(array(
		    	'diary_id'  => $diaryId,
			    'url'   => $res['image']['relative'],
			    'time'  => date('Y-m-d H:i:s')
		    ));
	    }
        if($diaryId){
	        return $this->success('操作成功！');
        }else{
	        return $this->error('操作失败！');
        }
    }

	/**
	 * 回复
	 */
	function reply(){
		$did = $this->request->get['did'];
		if(!is_numeric($did)){
			$this->error('获取did失败!');
		}
		$info = Db::name('diary d')
			->field('d.*, p.url, u.avatar, u.nickname, b.name as book_name')
			->join('diary_pic p', 'p.diary_id = d.id', 'left')
			->join('book b', 'd.book_id = b.id', 'left')
			->join('user u', 'd.uid = u.id', 'left')
			->find();
		$this->set('info', $info);
		return $this->fetch();
	}

	/**
	 * 回复列表
	 * @return array
	 */
	function _commentList(){
		$did = $this->request->data['did'];
		if(!is_numeric($did)){
			$this->error('获取did失败!');
		}
		$page = $this->request->data['pageNum'] ? $this->request->data['pageNum'] : 1;
		$list = Db::name('comment c')
			->field('c.*, u.nickname, u.avatar')
			->join('user u', 'u.id = c.uid')
			->where(['diary_id'=>$did])
			->order('c.id')
			->page($page, 15)
			->select();
		return Response::success('ok', $list);
	}

    /**
     * 日记回复操作
     */
    function _reply(){
        $params = $this->request->post;
        params_check(array('id', 'content'));
        if(!Session::get('uid')){
            return Response::error('获取UID失败!');
        }
        if(!is_numeric($params['id'])){
            return Response::error('获取日记的id失败!');
        }
        if(empty($params['content'])){
            return Response::error('回复内容不能为空!');
        }
        if(Db::name('comment')->insert(array(
            'diary_id'  => $params['id'],
            'uid'  => Session::get('uid'),
            'content'  => Db::quote($params['content']),
            'time'  => date('Y-m-d H:i:s')
        ))){
            return Response::success('success!', 1);
        }else{
            return Response::error('写入数据库失败', 2);
        }
    }

}